﻿using System;
using System.Collections;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using Googleboys.Inventa.Interfaces;
using Googleboys.Inventa.Main;
using Googleboys.Inventa.Misc;
using Googleboys.Inventa.Persistancy;
using Googleboys.Inventa.Properties;

namespace Googleboys.Inventa.Main
{
	public partial class frmInventa : Form
	{
		//
		// Member
		//
		#region Member

		private FormState _CurrentFormState = new FormState();
		private readonly Hashtable _RegisteredForms = new Hashtable();

		private frmTastaturKuerzel _frmKuerzel;
		private frmAbout _frmAbout;
		private frmStatus _frmStatus;

		private Form _CurrentForm;

		#endregion



		//
		// Konstruktor
		//
		#region Konstruktor

		public frmInventa()
		{
			// Initialisieren des MDI Fensters
			InitializeComponent();


			// Start des aufbaus des Objektmodells
			dbConnect.CreateObjectModel();

			// Aktivieren der Toolbar
			FormState.FormInventa = this;
			FormState.buttons = this;
			FormState.enableMenu();


			// Status Fenster
			loadInvStatus();
		}

		#endregion



		/// <summary>
		/// Laden der OmniCalc Module
		/// </summary>
		#region Menuitem_event

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			loadInvUeber();
		}

		private void tastaturkürzelToolStripMenuItem_Click(object sender, EventArgs e)
		{
			loadInvKuerzel();
		}

		#endregion



		//
		// Funktionen zum instanziieren der Einzelnen Fenster
		//
		#region Load Funktionen

		private void loadInvStatus()
		{
			_frmStatus = (frmStatus)RegisterForm("Main.frmStatus");
			_frmStatus.Show();
		}

		private void loadInvUeber()
		{
			FrmAbout.WindowState = FormWindowState.Normal;
			FrmAbout.ShowDialog();
		}

		private void loadInvKuerzel()
		{
			FrmKuerzel.WindowState = FormWindowState.Normal;
			FrmKuerzel.ShowDialog();
		}
		
		#endregion



		//
		// Modul Properties
		//
		#region Modul Properties

		public frmAbout FrmAbout
		{
			get
			{
				if (_frmAbout == null || _frmAbout.IsDisposed)
				{
					_frmAbout = new frmAbout();
				}

				return _frmAbout;
			}
		}

		public frmTastaturKuerzel FrmKuerzel
		{
			get
			{
				if (_frmKuerzel == null || _frmKuerzel.IsDisposed)
				{
					_frmKuerzel = new frmTastaturKuerzel();
				}

				return _frmKuerzel;
			}
		}

		private void Child_UpdateCurrentForm(object sender, EventArgs e)
		{
			Form form = sender as Form;
			this._CurrentForm = form;
			this._CurrentFormState = FormState.ConfigureToolStrip(form);
		}

		private void Child_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void SetupForm(Form form)
		{
			this._CurrentForm = form;
			FormState.ConfigureToolStrip(form);
		}

		#endregion



		//
		// Toolstripbutton Properties
		//
		#region Toolstripbutton Properties

		public ToolStripPanel TlStrpPnlTop
		{
			get { return tlStrpPnlTop; }
			set { tlStrpPnlTop = value; }
		}

		public ToolStripButton TlStrpBttnMainNew
		{
			get { return tlStrpBttnMainNew; }
			set { tlStrpBttnMainNew = value; }
		}

		public ToolStripButton TlStrpBttnMainSave
		{
			get { return tlStrpBttnMainSave; }
			set { tlStrpBttnMainSave = value; }
		}

		public ToolStripButton TlStrpBttnMainHelp
		{
			get { return tlStrpBttnMainHelp; }
			set { tlStrpBttnMainHelp = value; }
		}

		public ToolStripButton TlStrpBttnMainEdit
		{
			get { return tlStrpBttnMainEdit; }
			set { tlStrpBttnMainEdit = value; }
		}

		public ToolStripButton TlStrpBttnMainDelete
		{
			get { return tlStrpBttnMainDelete; }
			set { tlStrpBttnMainDelete = value; }
		}

		public ToolStripButton TlStrpBttnMainSaveAll
		{
			get { return tlStrpBttnMainSaveAll; }
			set { tlStrpBttnMainSaveAll = value; }
		}

		#endregion



		//
		// Statusbar Properties
		//
		#region Statusbar Properties

		public Form CurrentForm
		{
			get
			{
				return _CurrentForm;
			}
			set
			{
				_CurrentForm = value;
			}
		}

		public FormState CurrentFormState
		{
			get { return _CurrentFormState; }
			set { _CurrentFormState = value; }
		}

		public ToolStripStatusLabel TlStrpSttsLblBenutzer
		{
			get
			{
				return this.tlStrpSttsLblBenutzer;
			}
		}

		public String StatusText
		{
			get
			{
				return _CurrentFormState.StatusText;
			}
			set
			{
				this.tlStrpSttsLblStatus.Text = value;
				_CurrentFormState.StatusText = value;
			}
		}

		public Image StatusImage
		{
			get
			{
				return _CurrentFormState.StatusImage;
			}
			set
			{
				tlStrpSttsLblImage.Image = value;
				_CurrentFormState.StatusImage = value;
			}
		}


		#endregion



		//
		// Menubar Properties
		//
		#region Menubar Properties

		public ToolStrip TlStrpFunktion
		{
			get { return tlStrpFunktion; }
			set { tlStrpFunktion = value; }
		}

		public ToolStripMenuItem EditMenu
		{
			get { return editMenu; }
			set { editMenu = value; }
		}

		public ToolStripMenuItem ViewMenu
		{
			get { return viewMenu; }
			set { viewMenu = value; }
		}

		public ToolStripMenuItem ToolsMenu
		{
			get { return toolsMenu; }
			set { toolsMenu = value; }
		}

		public ToolStripMenuItem WindowsMenu
		{
			get { return windowsMenu; }
			set { windowsMenu = value; }
		}

		public ToolStripMenuItem HelpMenu
		{
			get { return helpMenu; }
			set { helpMenu = value; }
		}

		public ToolStripMenuItem InventarMenu
		{
			get { return inventarMenu; }
			set { inventarMenu = value; }
		}

		#endregion



		// 
		// Show Funktionen
		//
		#region Show Funktionen

		/// <summary>
		/// Zeigt eine Meldung in der Statusleiste an.
		/// </summary>
		/// <param name="message">Meldung</param>
		public void ShowStatus(String message)
		{
			StatusImage = null;
			StatusText = message;
		}



		/// <summary>
		/// Zeigt eine Meldung in der Statusleiste sowie das Bild an.
		/// </summary>
		/// <param name="message">Meldung</param>
		/// <param name="image">Bild, das vor dem Text angezeigt werden soll</param>
		public void ShowStatus(String message, Image image)
		{
			StatusImage = image;
			StatusText = message;
		}



		/// <summary>
		/// Zeigt eine Erfolgsmeldung in der Statusleiste sowie das Erfolgssymbol an.
		/// </summary>
		/// <param name="message">Erfolgsmeldung</param>
		public void ShowSuccess(String message)
		{
			StatusImage = Resources.IconOk;
			StatusText = message;
		}

		#endregion



		//
		// Menuitem Click
		//
		#region Menuitem Click

		private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void CutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			// TODO: Mithilfe von System.Windows.Forms.Clipboard den ausgewählten Text bzw. die ausgewählten Bilder in die Zwischenablage kopieren
		}

		private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
		{
			// TODO: Mithilfe von System.Windows.Forms.Clipboard den ausgewählten Text bzw. die ausgewählten Bilder in die Zwischenablage kopieren
		}

		private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			// TODO: Mithilfe von System.Windows.Forms.Clipboard.GetText() oder System.Windows.Forms.GetData Informationen aus der Zwischenablage abrufen.
		}

		private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
		{
			tlStrpFunktion.Visible = toolBarToolStripMenuItem.Checked;
		}

		private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
		{
			statusStrip.Visible = statusBarToolStripMenuItem.Checked;
		}

		private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.Cascade);
		}

		private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.TileVertical);
		}

		private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.TileHorizontal);
		}

		private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.ArrangeIcons);
		}

		private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
		{
			foreach (Form childForm in MdiChildren)
			{
				childForm.Close();
			}
		}

		private void newToolStripButton_Click(object sender, EventArgs e)
		{
			((IMdiEventHandler)_CurrentForm).ButtonNewClick();
		}

		private void saveToolStripButton_Click(object sender, EventArgs e)
		{
			((IMdiEventHandler)_CurrentForm).ButtonSaveClick();
		}

		private void tlStrpBttnBearbeiten_Click(object sender, EventArgs e)
		{
			((IMdiEventHandler)_CurrentForm).ButtonEditClick();
		}

		private void tlStrpBttnLoeschen_Click(object sender, EventArgs e)
		{
			((IMdiEventHandler)_CurrentForm).ButtonDeleteClick();
		}

		private void helpToolStripButton_Click(object sender, EventArgs e)
		{
			loadInvKuerzel();
		}

		private void newToolStripMenuItem_Click(object sender, EventArgs e)
		{
			((IMdiEventHandler)_CurrentForm).ButtonNewClick();
		}

		private void saveToolStripMenuItem_Click(object sender, EventArgs e)
		{
			((IMdiEventHandler)_CurrentForm).ButtonSaveClick();
		}

		private void tlStrpBttnMainSaveAll_Click(object sender, EventArgs e)
		{
			((IMdiEventHandler)_CurrentForm).ButtonSaveAllClick();
		}

		private void alleSpeichernToolStripMenuItem_Click(object sender, EventArgs e)
		{
			((IMdiEventHandler)_CurrentForm).ButtonSaveAllClick();
		}

		private void softwareToolStripMenuItem_Click(object sender, EventArgs e)
		{
			frmStatus status = this.MdiChildren[0] as frmStatus;

			if (status == null)
				return;

			status.ShowSoftwarePanel();
		}

		private void hardwareToolStripMenuItem_Click(object sender, EventArgs e)
		{
			frmStatus status = this.MdiChildren[0] as frmStatus;

			if (status == null)
				return;

			status.ShowHardwarePanel();
		}

		private void mobiliarToolStripMenuItem_Click(object sender, EventArgs e)
		{
			frmStatus status = this.MdiChildren[0] as frmStatus;

			if (status == null)
				return;

			status.ShowMobiliarPanel();
		}

		private void bücherToolStripMenuItem_Click(object sender, EventArgs e)
		{
			frmStatus status = this.MdiChildren[0] as frmStatus;

			if (status == null)
				return;

			status.ShowBuecherPanel();
		}

		private void personenToolStripMenuItem_Click(object sender, EventArgs e)
		{
			frmStatus status = this.MdiChildren[0] as frmStatus;

			if (status == null)
				return;

			status.ShowPersonenPanel();
		}

		private void orteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			frmStatus status = this.MdiChildren[0] as frmStatus;

			if (status == null)
				return;

			status.ShowOrtePanel();
		}

		#endregion



		/// <summary>
		/// Instanziiert ein neues Objekt des übergebenen Typs "formName" und weist 
		/// </summary>
		/// <param name="formName">Name des zu instanziierenden Objekts</param>
		/// <param name="objects">Konstruktorparameter.</param>
		/// <returns></returns>
		public Form RegisterForm(String formName, params Object[] objects)
		{
			Form form;
			try
			{
				if (_RegisteredForms.Contains(formName) && !((Form)_RegisteredForms[formName]).IsDisposed)
					((Form)_RegisteredForms[formName]).Close();

				if (!_RegisteredForms.Contains(formName) || ((Form)_RegisteredForms[formName]).IsDisposed)
				{
					if (_RegisteredForms.Contains(formName) && ((Form)_RegisteredForms[formName]).IsDisposed)
						_RegisteredForms.Remove(formName);
					Assembly assembly = Assembly.GetCallingAssembly();
					Type controlType = assembly.GetType("Googleboys.Inventa." + formName, true);
					_RegisteredForms.Add(formName, Activator.CreateInstance(controlType, objects));
					form = (Form)_RegisteredForms[formName];
					form.Activated += Child_UpdateCurrentForm;
					form.FormClosed += Child_FormClosed;
					SetupForm(form);
					this.menuStrip.MdiWindowListItem = this.windowsMenu;
				}
			}
			catch (Exception e)
			{
				if (e.InnerException != null)
					e = e.InnerException;

				Console.Out.WriteLine("Could not instanciate class " + formName + e.Message + e.StackTrace);
				throw new Exception("Could not instanciate class " + formName + e.Message + e.StackTrace);
			}

			form = (Form)_RegisteredForms[formName];
			form.MdiParent = this;
			form.WindowState = FormWindowState.Maximized;
			form.BringToFront();
			return form;
		}
	}
}
